#import "SMRScopeBarTestAppDelegate.h"
#import "SMRScopeBarFramework.h"
#import <FScript/FScript.h>


@implementation SMRScopeBarTestAppDelegate

@synthesize window;
@synthesize scopeBar;

-(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [[NSApp mainMenu] addItem:[[FScriptMenuItem alloc] init]];
    
    SMRScopeBarGroup *allGroup = [[self scopeBar] addGroup];
    [allGroup setIdentifier:@"all"];
    [allGroup setGroupSelectionStyle:SMRScopeBarGroupSelectionStyleRadio];

    SMRScopeBarGroup *sectionGroup = [[self scopeBar] addGroup];
    [sectionGroup setIdentifier:@"section"];
    [sectionGroup setGroupSelectionStyle:SMRScopeBarGroupSelectionStyleMultipleSelection];
    
    SMRScopeBarGroup *customGroup = [[self scopeBar] addGroup];
    [customGroup setIdentifier:@"custom"];
    [customGroup setGroupSelectionStyle:SMRScopeBarGroupSelectionStyleRadio];
    
    NSArray *spanGroups = [NSArray arrayWithObjects:allGroup, sectionGroup, customGroup, nil];
    NSLog(@"spanGroups %@", spanGroups);
    [[self scopeBar] setSpanSelectionGroups:spanGroups];
    [[self scopeBar] reloadData];
}


#pragma mark -
#pragma mark Data source

-(NSUInteger)scopeBar:(SMRScopeBar *)scopeBar numberOfObjectsInScopeBarGroup:(NSUInteger)groupIndex {
    if (groupIndex == 1)
        return 2;
    return 1;
}

-(id)scopeBar:(SMRScopeBar *)scopeBar valueForObjectInGroup:(NSUInteger)groupIndex atIndex:(NSUInteger)objectIndex {
    if (groupIndex == 0)
        return @"All";
    if (groupIndex == 2)
        return @"Custom";
    if (objectIndex == 0)
        return @"First";
    return @"Second";
}


#pragma mark -
#pragma mark Delegate

-(SEL)scopeBar:(SMRScopeBar *)scopeBar actionForObject:(id)object inGroup:(SMRScopeBarGroup *)group {
    NSLog(@"action for group %@", [group identifier]);
    return @selector(scopeBarAction:);
}

-(void)scopeBarAction:(id)sender {
    NSLog(@"action called for %@", [sender identifier]);
}

//-(void)scopeBarSelectionDidChange:(NSNotification *)notification {
//    NSDictionary *userInfo = [notification userInfo];
//    NSString *affectedGroupIdentifier = [userInfo valueForKey:SMRGroupIdentifierKey];
//    
//    SMRScopeBarGroup *affectedGroup = [[self scopeBar] groupWithIdentifier:affectedGroupIdentifier];
//    SMRScopeBarGroup *allGroup = [[self scopeBar] groupWithIdentifier:@"all"];
//    SMRScopeBarGroup *sectionGroup = [[self scopeBar] groupWithIdentifier:@"section"];
//    
//    NSIndexSet *affectedSelection = [affectedGroup selectionIndexes];
//    NSIndexSet *oldAffectedSelection = [userInfo valueForKey:SMROldSelectionIndexesKey];
//    if ([affectedSelection count] < [oldAffectedSelection count])
//        return;
//    
//    if ([affectedGroupIdentifier isEqualToString:@"all"])
//        [sectionGroup deselectAll];
//    else {
//        [allGroup deselectAll];
//        NSMutableIndexSet *mutableAffectedSelection = [affectedSelection mutableCopy];
//        [mutableAffectedSelection removeIndexes:oldAffectedSelection];
//        [sectionGroup setSelectionIndexes:mutableAffectedSelection];
//    }
//}

@end
